﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;

using ToDo_CSharp.Properties;

using forms = System.Windows.Forms;
using drawing = System.Drawing;

namespace ToDo_CSharp
{
	/// <summary>
	/// Interaction logic for Configuration.xaml
	/// </summary>
	public partial class Configuration : Window
	{
		public Configuration()
		{
			InitializeComponent();
			SetToDoFilePathText();
		}

		private void fontColor_Click(object sender, RoutedEventArgs e)
		{
			var colorDialog = GetColorDialog(Settings.Default.FontForeColor);

			if (colorDialog.ShowDialog() == forms.DialogResult.OK)
			{
				Settings.Default.FontForeColor = TranslateColor(colorDialog.Color);
			}
		}

		private void glowColor_Click(object sender, RoutedEventArgs e)
		{
			var colorDialog = GetColorDialog(Settings.Default.FontGlowColor);

			if (colorDialog.ShowDialog() == forms.DialogResult.OK)
			{
				Settings.Default.FontGlowColor = TranslateColor(colorDialog.Color);
			}
		}

		private void btnToDoFind_Click(object sender, RoutedEventArgs e)
		{
			var fileDialog = new forms.OpenFileDialog
			                 	{
			                 		FileName =
			                 			(!File.Exists(Settings.Default.ToDoFileName)
			                 			 	? Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
			                 			 	: Settings.Default.ToDoFileName)
			                 	};

			if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				var fi = new FileInfo(fileDialog.FileName);

				Settings.Default.ToDoFileName = fi.Name;
				Settings.Default.ToDoFilePath = fi.DirectoryName;

				SetToDoFilePathText();
			}
		}

		private static forms.ColorDialog GetColorDialog(Color color)
		{
			return new forms.ColorDialog
			{
				Color =
					drawing.ColorTranslator.FromHtml(string.Format("#{0}{1}{2}", color.R.ToString("X"),
					color.G.ToString("X"), color.B.ToString("X")))
			};
		}

		private static Color TranslateColor(drawing.Color value)
		{
			return Color.FromArgb(value.A, value.R, value.G, value.B);
		}

		private void SetToDoFilePathText()
		{
			txtToDoFind.Text = Path.Combine(Settings.Default.ToDoFilePath, Settings.Default.ToDoFileName);
		}


		#region ok / cancel buttons

		private void btnhCancel_Click(object sender, RoutedEventArgs e)
		{
			btnOk.IsEnabled = false;

			Settings.Default.Reload();

			DialogResult = false;
		}

		private void btnOk_Click(object sender, RoutedEventArgs e)
		{
			btnOk.IsEnabled = false;

			Settings.Default.Save();

			DialogResult = true;
		}
		#endregion
	}
}
